Changing the Background Color of a Table

The bgcolor attribute of the <table> tag is used to change the background color for the entire table. You can also change the color for the entire row by inserting the bgcolor attribute inside the <tr> tag. The bgcolor attribute can also be used to change the color for the individual cell of a table by inserting it inside the >td> tag.

To specify a color for the bgcolor attribute, you can either use an RGB (Red, Green and Blue) color value, such as #33dd33 and #33ddaa, or color name, such as while and yellow. By default, the background color of the table is while.

Let’s do the following steps to set the background color of a table.


<!DOCTYPE html>
<head>
    <title>Setting the Width of Table Columns</title>
</head>
<body bgcolor=”alicablue”>
<table border=”1” align=”right” width=”95%” bgcolor=”gray”>
<caption>
    <h2>Student Details</h2>
</caption
<tr>
    <th width=”20%”>Name</th>
    <th width=”20%”>Date of Birth</th>
    <th width=”60%”>Address</th>
</tr>
    <tr>
        <td align=”center”>Manish Kumar</td>
        <td> 15-03-1983</td>
        <td align=”center”>Flat No, 303, Shipra Suncity, Ghaziabad</td>
    </tr>
    <tr>
        <td align=”center”>Rajesh Gupta </td>
        <td>22-02-1984</td>
        <td align=”center”> H. No.-32, Rajendra Place, New Delhi</td>
    </tr>
    <tr>
        <td align=”center”>Manisha Dubey </td>
        <td>05-02-1995</td>
        <td align=”center”> H. No.-125, Patel Nagar, New Delhi</td>
    </tr>
</table>
</body>
</html>

Save the document the name of BackgroundColor.html. Open the document on browser.

Setting bgcolor in table in html 5